<?php

/**
 * Classe MyBdT
 */
class MyDB_Sql extends DB_Sql {
  var $Host     = 'localhost';
  var $Database = 'gestion';
  var $User     = 'root';
  var $Password = '';

  var $Halt_On_Error = "report";

  // MyDB_Sql::insert_id
  function insert_id()
  {
    return @mysqli_insert_id($this->Link_ID);
  }
}

/*
 * Classe MyCT_Sql
 */
class MyCT_Sql extends CT_Sql {
  var $database_class = 'MyDB_Sql';          # Which database to connect...
  var $database_table = 'active_sessions'; # and find our session data in this table.
}

/*
 * Classe MySession
 */
class MySession extends Session {
  var $classname      = 'MySession';

  var $cookiename     = '';                # defaults to classname
  var $magic          = 'Hocuspocus';      # ID seed
  var $mode           = 'cookies';          # We propagate session IDs with cookies
  var $fallback_mode  = 'get';
  var $lifetime       = 0;                 # 0 = do session cookies, else minutes
  var $that_class     = 'MyCT_Sql';        # name of data storage container class
  var $gc_probability = 5;  
  var $allowcache     = 'no';              # "public", "private", or "no"
}

/*
 * Classe MyUser
 */
class MyUser extends User {
  var $classname      = 'MyUser';

  var $magic          = 'Abracadabra'; # ID seed
  var $that_class     = 'MyCT_Sql';    # name of data storage container class
}

/*
 * Classe MyAuth
 */
class MyAuth extends Auth {
  var $classname      = 'MyAuth';
  var $lifetime       =  60;           # minutes inactives avant deconnexion auto
  var $magic          = 'Frobozzica';  # Challenge seed
  var $database_class = 'MyDB_Sql';
  var $database_table = 'auth_user';

  // MyAuth::auth_loginform
  function auth_loginform()
  {
    $TPL['uname'] = isset($this->auth['uname']) ? $this->auth['uname'] : '';
    
    require('login.html.php');
  }
  
  // MyAuth::auth_validatelogin
  function auth_validatelogin()
  {
    $this->auth['uname'] = rd_get('username', $this->auth['uname']);
    
    $uid = false;
    
    $req = sprintf( "SELECT user_id, perms FROM %s"
                   ." WHERE username='%s' AND password=MD5('%s');"
                  , $this->database_table
                  , addslashes(rd_get('username'))
                  , addslashes(rd_get('password')) );
    $this->db->query($req);

    while($this->db->next_record())
    {
      $uid = $this->db->f('user_id');
      $this->auth['perm'] = $this->db->f('perms');
    }
    
    return $uid;
  }
}

/*
 * Classe MyPerm
 */
class MyPerm extends Perm {
  var $classname = 'MyPerm';
  var $permissions = array( 'admin' => 1 );
  
  // MyPerm::perm_invalid
  function perm_invalid($does_have, $must_have)
  {
    global $perm, $auth, $sess;
    
    require('perminvalid.html.php');
  }
 
  function perm_sel($name, $current = "", $id = "")
  {
    reset($this->permissions);
    
    $ret = sprintf( '<select multiple="multiple" name="%s[]"%s>'
                  , $name
                  , $id ? " id='$id'" : ''
                  );
    while (list($k, $v) = each($this->permissions))
    {
      $ret .= sprintf( '<option value="%s"%s>%s</option>'
                     , $v
                     , $this->perm_islisted($current, $k) ? ' selected="selected"' : ''
                     , $k
                     );
    }
    $ret .= "</select>";

    return $ret;
  }
}
?>
